package com.zlsy.es.dianping.controller;

import com.zlsy.es.dianping.base.R;
import com.zlsy.es.dianping.baseexception.BizException;
import com.zlsy.es.dianping.constan.ErrorStatusEnum;
import com.zlsy.es.dianping.pojo.model.Category;
import com.zlsy.es.dianping.pojo.vo.response.ShopResponse;
import com.zlsy.es.dianping.service.CategoryService;
import com.zlsy.es.dianping.service.EsService;
import com.zlsy.es.dianping.service.ShopService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouliang
 */
@Slf4j
@Controller
@RequestMapping("/shop")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShopIndexController {

    private final ShopService shopService;
    private final CategoryService categoryService;
    private final EsService esService;

    /**
     * 推荐服务V1.0
     *
     * @param longitude
     * @param latitude
     * @return
     */
    @RequestMapping("/recommend")
    @ResponseBody
    public R recommend(@RequestParam(name = "longitude") BigDecimal longitude,
                       @RequestParam(name = "latitude") BigDecimal latitude) {
        if (longitude == null || latitude == null) {
            throw new BizException(ErrorStatusEnum.PARAMETER_VALIDATION_ERROR);
        }
        return R.success(shopService.recommend(longitude, latitude));
    }


    /**
     * 搜索服务V1.0
     *
     * @param longitude
     * @param latitude
     * @param keyword
     * @param orderby
     * @param categoryId
     * @param tags
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public R search(@RequestParam(name = "longitude") BigDecimal longitude,
                    @RequestParam(name = "latitude") BigDecimal latitude,
                    @RequestParam(name = "keyword") String keyword,
                    @RequestParam(name = "orderby", required = false) Integer orderby,
                    @RequestParam(name = "categoryId", required = false) Integer categoryId,
                    @RequestParam(name = "tags", required = false) String tags) {
        if (StringUtils.isEmpty(keyword) || longitude == null || latitude == null) {
            throw new BizException(ErrorStatusEnum.PARAMETER_VALIDATION_ERROR);
        }

        Map<String, Object> map = esService.searchES(longitude, latitude, keyword, orderby, categoryId, tags);
        List<ShopResponse> shopResponseList = (List<ShopResponse>) map.get("shop");
        List<Map<String, Object>> tagsAggregation = (List<Map<String, Object>>) map.get("tags");


//        List<ShopResponse> shopResponseList = shopService.search(longitude, latitude, keyword, orderby, categoryId, tags);
        List<Category> categoryModelList = categoryService.selectAll();
//        List<Map<String, Object>> tagsAggregation = shopService.searchGroupByTags(keyword, categoryId, tags);
        Map<String, Object> resMap = new HashMap<>(16);
        resMap.put("shop", shopResponseList);
        resMap.put("category", categoryModelList);
        resMap.put("tags", tagsAggregation);

        return R.success(resMap);

    }


}
